package com.yhp.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yhp.bean.Student;
import com.yhp.dao.StudentDao;
import com.yhp.dao.impl.StudentDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet(value = "/insert.do")
public class InsertServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        int a1 = Integer.parseInt(req.getParameter("n1"));
        String a2 = req.getParameter("n2");
        String a3 = req.getParameter("n3");
//        System.out.println(a3);
        int a4 = Integer.parseInt(req.getParameter("n4"));
        String a5 = req.getParameter("n5");
        String a6 = req.getParameter("n6");
        String a7 = req.getParameter("n7");
        String a8 = req.getParameter("n8");
        String a9 = req.getParameter("n9");
        String a10 = req.getParameter("n10");
        String a11 = req.getParameter("n11");
        //String a12 = req.getParameter("n12");日期
        Date a12 = new Date();
        int a13 = Integer.parseInt(req.getParameter("n13"));
        String a14 = req.getParameter("n14");
        Student s = new Student();
//        s.setStuId(null);
        s.setStuName(a2);
        s.setStuNo(a3);
        s.setSex(a4);
        s.setPhone(a5);
        s.setEmail(a6);
        s.setRegistered(a7);
        s.setAddress(a8);
        s.setProfession(a9);
        s.setIdNumber(a10);
        s.setPolitics(a11);
        s.setRegDate(a12);
        s.setState(a13);
        s.setIntrodction(a14);
        StudentDao s1 = new StudentDaoImpl();
        int a = s1.insert(s);
        Map<String, Object> model = new HashMap<String, Object>();
        if(a==1){
            model.put("success",true);
        }
        if(a==0){
            model.put("success",false);
        }
        resp.setContentType("application/json; charset=UTF-8");
        PrintWriter out=resp.getWriter();

        ObjectMapper objectMapper=new ObjectMapper();
        out.println(objectMapper.writeValueAsString(model));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
